package com.lu.adog.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.support.annotation.Nullable;

import java.util.Map;
import java.util.Set;

/**
 * 偏好设置工具类
 * Created by lu on 2017/8/31 13:55
 */
public class SharedPreferencesUtil {
    public static Editor editor(Context context, String shareName) {
        SharedPreferences preferences = context.getSharedPreferences(shareName, Context.MODE_PRIVATE);
        return preferences.edit();
    }

    public static Editor editor(Context context, String shareName, int sharedPreferencesMode) {
        SharedPreferences preferences = context.getSharedPreferences(shareName, sharedPreferencesMode);
        return preferences.edit();
    }

    public static void editor(Context context, String shareName, CallBack callBack) {
        SharedPreferences sp = context.getSharedPreferences(shareName, Context.MODE_PRIVATE);
        callBack.call(new SharedElements(sp));
    }

    public static void editor(Context context, String shareName, int sharedPreferencesMode, CallBack callBack) {
        SharedPreferences preferences = context.getSharedPreferences(shareName,sharedPreferencesMode);
        callBack.call(new SharedElements(preferences));
    }

    //保存
    public static void save(Context context ,String shareName,CallBack callBack){
        save(context,shareName,Context.MODE_PRIVATE,callBack);
    }

    public static void save(Context context ,String shareName,int sharedPreferencesMode,CallBack callBack){
        SharedPreferences preferences = context.getSharedPreferences(shareName, sharedPreferencesMode);
        Editor editor = preferences.edit();
        if (callBack != null) {
            callBack.call(new SharedElements(preferences));
        }
        editor.commit();
    }

    public static void search(Context context, String shareName,CallBack callBack){
        search(context,shareName,Context.MODE_PRIVATE,callBack);
    }

    public static void search(Context context, String shareName, int sharedPreferencesMode, CallBack callBack){
        SharedPreferences preferences = context.getSharedPreferences(shareName, sharedPreferencesMode);
        Editor editor = preferences.edit();
        if (callBack != null) {
            callBack.call(new SharedElements(preferences));
        }
    }
    public static SharedPreferences sharedPreferences(Context context, String shareName){
        return context.getSharedPreferences(shareName,Context.MODE_PRIVATE);
    }

    public static SharedPreferences sharedPreferences(Context context, String shareName, int sharedPreferencesMode){
        return context.getSharedPreferences(shareName, sharedPreferencesMode);
    }
    public interface CallBack {
        void call(SharedElements sharedElements);

    }

    //代理类
    public static class SharedElements{
        private Editor editor;
        private SharedPreferences sp;

        public SharedElements(SharedPreferences sp) {
            editor = sp.edit();
            this.sp = sp;
        }

        public Editor getEditor() {
            return editor;
        }

        public SharedPreferences getSp() {
            return sp;
        }

        public Map<String, ?> getAll() {
            return sp.getAll();
        }

        public String getString(String key, @Nullable String defValue) {
            return sp.getString(key, defValue);
        }

        public Set<String> getStringSet(String key, @Nullable Set<String> defValues) {
            return sp.getStringSet(key, defValues);
        }

        public int getInt(String key, int defValue) {
            return sp.getInt(key, defValue);
        }

        public long getLong(String key, long defValue) {
            return sp.getLong(key, defValue);
        }

        public float getFloat(String key, float defValue) {
            return sp.getFloat(key, defValue);
        }

        public boolean getBoolean(String key, boolean defValue) {
            return sp.getBoolean(key, defValue);
        }

        public boolean contains(String key) {
            return sp.contains(key);
        }

        public Editor edit() {
            return sp.edit();
        }

        public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
            sp.registerOnSharedPreferenceChangeListener(listener);
        }

        public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
            sp.unregisterOnSharedPreferenceChangeListener(listener);
        }

        public Editor putString(String key, @Nullable String value) {
            return editor.putString(key, value);
        }

        public Editor putStringSet(String key, @Nullable Set<String> values) {
            return editor.putStringSet(key, values);
        }

        public Editor putInt(String key, int value) {
            return editor.putInt(key, value);
        }

        public Editor putLong(String key, long value) {
            return editor.putLong(key, value);
        }

        public Editor putFloat(String key, float value) {
            return editor.putFloat(key, value);
        }

        public Editor putBoolean(String key, boolean value) {
            return editor.putBoolean(key, value);
        }

        public Editor remove(String key) {
            return editor.remove(key);
        }

        public Editor clear() {
            return editor.clear();
        }

        public boolean commit() {
            return editor.commit();
        }

        public void apply() {
            editor.apply();
        }
    }
}
